use actix_web::{web, HttpResponse, Responder};
use diesel::prelude::*;
use crate::db::schema::tasks::dsl::*;

pub async fn delete_task(
    pool: web::Data<db::DbPool>,
    task_id: web::Path<i32>,
) -> impl Responder {
    let conn = pool.get().expect("Couldn't get db connection from pool");

    let num_deleted = web::block(move || {
        diesel::delete(tasks.find(task_id.into_inner())).execute(&conn)
    })
    .await
    .map_err(|e| {
        eprintln!("{}", e);
        HttpResponse::InternalServerError().finish()
    })?;

    if num_deleted > 0 {
        Ok(HttpResponse::NoContent().finish())
    } else {
        Ok(HttpResponse::NotFound().finish())
    }
}
